<?php
/**
 * utilUipackDriverJquery
 *
 *
 * @package     util
 * @subpackage  Uipack
 * @author      Platform Team
 */

class utilUipackDriverJquery extends utilUipackDriverAbstract
{
    /**
     * 기본 루트 디렉토리 지정
     *
     * @return string
     */
    protected function setDefaultRootDir()
    {
        return FW_DIR_FRAMEWORK_UIPACK . DS . UIPACK_DRIVER_JQUERY;
    }

    /**
     * 루트 디렉토리 지정
     *
     * @return string
     */
    protected function setRootDir()
    {
        return FW_DIR_FRAMEWORK_UIPACK . DS . UIPACK_DRIVER_JQUERY;
    }

    /**
     * JQuery UI 용 CSS 삽입
     */
    private function insertJqueryUiCss()
    {
        $this->css_($this->sDefaultRootdir, '/plugins/ui/css/jquery.ui.core');
        $this->css_($this->sDefaultRootdir, '/plugins/ui/css/jquery.ui.resizable');
        $this->css_($this->sDefaultRootdir, '/plugins/ui/css/jquery.ui.selectable');
        $this->css_($this->sDefaultRootdir, '/plugins/ui/css/jquery.ui.accordion');
        $this->css_($this->sDefaultRootdir, '/plugins/ui/css/jquery.ui.autocomplete');
        $this->css_($this->sDefaultRootdir, '/plugins/ui/css/jquery.ui.button');
        $this->css_($this->sDefaultRootdir, '/plugins/ui/css/jquery.ui.dialog');
        $this->css_($this->sDefaultRootdir, '/plugins/ui/css/jquery.ui.slider');
        $this->css_($this->sDefaultRootdir, '/plugins/ui/css/jquery.ui.tabs');
        $this->css_($this->sDefaultRootdir, '/plugins/ui/css/jquery.ui.datepicker');
        $this->css_($this->sDefaultRootdir, '/plugins/ui/css/jquery.ui.progressbar');
        $this->css_($this->sDefaultRootdir, '/plugins/ui/css/jquery.ui.theme');

    }

    /**
     * Core
     *
     * @return boolean
     */
    protected function coreCallback()
    {
        $this->js_($this->sDefaultRootdir, UIPACK_DRIVER_JQUERY_FILE_NAME, 'before');

        if (version_compare('1.8', UIPACK_DRIVER_JQUERY_VERSION) === -1) {
            $this->js_($this->sDefaultRootdir, 'jquery-migrate', 'before');
        }

        return true;
    }

    /**
     * Plugin - Drag & Drop
     *
     * @link http://jqueryui.com/demos/droppable/
     * @link http://jqueryui.com/demos/draggable/
     */
    public function pluginDragDrop()
    {
        $this->js_($this->sDefaultRootdir, '/plugins/ui/jquery.ui.core');
        $this->js_($this->sDefaultRootdir, '/plugins/ui/jquery.ui.widget');
        $this->js_($this->sDefaultRootdir, '/plugins/ui/jquery.ui.mouse');
        $this->js_($this->sDefaultRootdir, '/plugins/ui/jquery.ui.draggable');
        $this->js_($this->sDefaultRootdir, '/plugins/ui/jquery.ui.droppable');
    }

    /**
     * Plugin - Popup
     */
    public function pluginPopup($aOption)
    {
        $this->css_($this->sDefaultRootdir, '/plugins/popup/popup');
        $this->js_($this->sDefaultRootdir, '/plugins/popup/jquery.12r.popup');
    }

    /**
     * Plugin - Calendar
     * @param array $aOption
     */
    public function pluginCalendar($aOption)
    {
        $this->css_($this->sDefaultRootdir, '/plugins/calendar/calendar');
        $this->js_($this->sDefaultRootdir, '/plugins/calendar/jquery.12r.calendar');
    }

    /**
     * plugin - ColorPicker
     * @param array $aColor
     */
    public function pluginColorPicker($aColor)
    {
        $this->css_($this->sDefaultRootdir, '/plugins/colorpicker/colorPicker');
        $this->js_($this->sDefaultRootdir, '/plugins/colorpicker/jquery.colorPicker');

        if ( ( $cnt = count($aColor) ) > 0 ) {
            $sColor = "";
            for ($i=0;$i<$cnt;$i++) {
                $sColor .= "'" . $aColor[$i] ."', ";
            }
            $sColor = rtrim($sColor,", ");
            $this->writeJS("$.fn.colorPicker.addColors([" .$sColor . "]);");
        }
    }

    /**
     * plugin - ColorPickerNew
     */
    public function pluginColorPickerNew()
    {
        $this->css_($this->sDefaultRootdir, '/plugins/colorpickernew/colorpicker');
        $this->js_($this->sDefaultRootdir, '/plugins/colorpickernew/colorpicker');
    }

    /**
     * plugin - DateUtil
     */
    public function pluginDateUtil()
    {
        $this->js_($this->sDefaultRootdir, '/plugins/dateutil/dateutil');
    }

    /**
     * plugin - javascript paging
     * @param array $aPaging
     */
    public function pluginPaging($aPaging)
    {
        $this->css_($this->sDefaultRootdir, '/plugins/paging/paging');

        $this->js_($this->sDefaultRootdir, '/plugins/paging/jquery.12r.paging');

        if (count($aPaging) > 0) {

            $aPaging['total'] = (!$aPaging['total']) ? "0" : $aPaging['total'] ;
            $aPaging['current_page'] = (!$aPaging['current_page']) ?"1": $aPaging['current_page'] ;
            $aPaging['paing_var']    = (!$aPaging['paing_var']) ? "page" :$aPaging['paing_var'] ;
            $aPaging['link_click']   = (!$aPaging['link_click']) ?  "false" : $aPaging['link_click'];
            $aPaging['addLink']      = (!$aPaging['addLink']) ? "" : $aPaging['addLink'] ;
            $aPaging['prev_text']    = (!$aPaging['prev_text']) ? "&lsaquo;" :  $aPaging['prev_text'];
            $aPaging['next_text']    = (!$aPaging['next_text']) ? "&rsaquo;" :  $aPaging['prev_text'];

            $this->writeJS("
            var opts= {
                    'total_record' : " .  $aPaging['total'] . ",  // 전체페이지 수
                    'current_page' : " .  $aPaging['current_page'] . ", //현재 페이지
                    'link_to' : '?" . $aPaging['addLink']  . "&" .$aPaging['paging_var'] . "=__id__' ,   //링크 경로 넘겨받을 변수 명을 넣으세요. __id__ 는 치환댈 페이지 번호입니다.
                    'prev_text'    : '" . $aPaging['prev_text'] . "',
                    'next_text'    : '" . $aPaging['next_text'] . "',
                    'prev_show_always' :true,
                    'next_show_always' :true ,
                    'block_size' : 10 ,   //블럭 사이즈
                    'addTag' : '',
                    'items_per_page' : " . $aPaging['items_per_page'] . ", //아이템 출력 건수
                    'link_click' : " . $aPaging['link_click']  . "
            };

            $('". $aPaging['name'] . "').paging(opts);
            ");

        }
    }

    /**
     * plugin - javascript Tooltip
     */
    public function pluginTooltip()
    {
        $this->css_($this->sDefaultRootdir, '/plugins/tooltip/tooltip');
        $this->js_($this->sDefaultRootdir, '/plugins/tooltip/jquery.12r.tooltip');
    }

    /**
     * plugin - javascript treeView
     */
    public function pluginTreeview()
    {
        $this->css_($this->sDefaultRootdir, '/plugins/treeview/jquery.treeview');

        $this->js_($this->sDefaultRootdir, '/externals/jquery.cookie');
        $this->js_($this->sDefaultRootdir, '/plugins/treeview/jquery.treeview');
    }

    /**
     * plugin - javascript Flash
     */
    public function pluginFlash()
    {
        $this->js_($this->sDefaultRootdir, '/plugins/flash/jquery.12r.flash');
    }

    /**
     * plugin - javascript Numberformat
     */
    public function pluginNumberformat()
    {
        $this->js_($this->sDefaultRootdir, '/plugins/numberformat/jquery.12r.numberformat');
    }

    /**
     * plugin - 비주얼 효과
     */
    public function pluginEffect()
    {
        $this->insertJqueryUiCss();

        $this->js_($this->sDefaultRootdir, '/plugins/ui/jquery.effects.core');
        $this->js_($this->sDefaultRootdir, '/plugins/ui/jquery.effects.blind');
        $this->js_($this->sDefaultRootdir, '/plugins/ui/jquery.effects.bounce');
        $this->js_($this->sDefaultRootdir, '/plugins/ui/jquery.effects.clip');
        $this->js_($this->sDefaultRootdir, '/plugins/ui/jquery.effects.drop');
        $this->js_($this->sDefaultRootdir, '/plugins/ui/jquery.effects.explode');
        $this->js_($this->sDefaultRootdir, '/plugins/ui/jquery.effects.fade');
        $this->js_($this->sDefaultRootdir, '/plugins/ui/jquery.effects.fold');
        $this->js_($this->sDefaultRootdir, '/plugins/ui/jquery.effects.highlight');
        $this->js_($this->sDefaultRootdir, '/plugins/ui/jquery.effects.pulsate');
        $this->js_($this->sDefaultRootdir, '/plugins/ui/jquery.effects.scale');
        $this->js_($this->sDefaultRootdir, '/plugins/ui/jquery.effects.shake');
        $this->js_($this->sDefaultRootdir, '/plugins/ui/jquery.effects.slide');
        $this->js_($this->sDefaultRootdir, '/plugins/ui/jquery.effects.transfer');
    }

    /**
     * plugin - 아코디언 메뉴
     */
    public function pluginAccordion()
    {
        $this->insertJqueryUiCss();

        $this->js_($this->sDefaultRootdir, '/plugins/ui/jquery.ui.core');
        $this->js_($this->sDefaultRootdir, '/plugins/ui/jquery.ui.widget');
        $this->js_($this->sDefaultRootdir, '/plugins/ui/jquery.ui.accordion');
    }

    /**
     * plugin - 탭 메뉴
     */
    public function pluginTab()
    {
        $this->insertJqueryUiCss();

        $this->js_($this->sDefaultRootdir, '/plugins/ui/jquery.ui.core');
        $this->js_($this->sDefaultRootdir, '/plugins/ui/jquery.ui.widget');
        $this->js_($this->sDefaultRootdir, '/plugins/ui/jquery.ui.tabs');
    }

    /**
     * plugin - 슬라이더
     */
    public function pluginSlider()
    {
        $this->insertJqueryUiCss();

        $this->js_($this->sDefaultRootdir, '/plugins/ui/jquery.ui.core');
        $this->js_($this->sDefaultRootdir, '/plugins/ui/jquery.ui.widget');
        $this->js_($this->sDefaultRootdir, '/plugins/ui/jquery.ui.mouse');
        $this->js_($this->sDefaultRootdir, '/plugins/ui/jquery.ui.slider');
    }

    /**
     * plugin - 다이얼로그
     */
    public function pluginDialog()
    {
        $this->insertJqueryUiCss();

        $this->js_($this->sDefaultRootdir, '/plugins/externals/jquery.bgiframe');
        $this->js_($this->sDefaultRootdir, '/plugins/ui/jquery.ui.core');
        $this->js_($this->sDefaultRootdir, '/plugins/ui/jquery.effects.core');
        $this->js_($this->sDefaultRootdir, '/plugins/ui/jquery.ui.widget');
        $this->js_($this->sDefaultRootdir, '/plugins/ui/jquery.ui.mouse');
        $this->js_($this->sDefaultRootdir, '/plugins/ui/jquery.ui.button');
        $this->js_($this->sDefaultRootdir, '/plugins/ui/jquery.ui.draggable');
        $this->js_($this->sDefaultRootdir, '/plugins/ui/jquery.ui.position');
        $this->js_($this->sDefaultRootdir, '/plugins/ui/jquery.ui.resizable');
        $this->js_($this->sDefaultRootdir, '/plugins/ui/jquery.ui.dialog');
    }

    /**
     * plugin - 정렬
     */
    public function pluginSortable()
    {
        $this->insertJqueryUiCss();

        $this->js_($this->sDefaultRootdir, '/plugins/ui/jquery.ui.core');
        $this->js_($this->sDefaultRootdir, '/plugins/ui/jquery.ui.widget');
        $this->js_($this->sDefaultRootdir, '/plugins/ui/jquery.ui.mouse');
        $this->js_($this->sDefaultRootdir, '/plugins/ui/jquery.ui.sortable');
    }

    /**
     * plugin - 선택
     */
    public function pluginSelectable()
    {
        $this->insertJqueryUiCss();

        $this->js_($this->sDefaultRootdir, '/plugins/ui/jquery.ui.core');
        $this->js_($this->sDefaultRootdir, '/plugins/ui/jquery.ui.widget');
        $this->js_($this->sDefaultRootdir, '/plugins/ui/jquery.ui.mouse');
        $this->js_($this->sDefaultRootdir, '/plugins/ui/jquery.ui.selectable');
    }

    /**
     * plugin - 사이즈 조절
     */
    public function pluginResizable()
    {
        $this->insertJqueryUiCss();

        $this->js_($this->sDefaultRootdir, '/plugins/ui/jquery.ui.core');
        $this->js_($this->sDefaultRootdir, '/plugins/ui/jquery.ui.widget');
        $this->js_($this->sDefaultRootdir, '/plugins/ui/jquery.ui.mouse');
        $this->js_($this->sDefaultRootdir, '/plugins/ui/jquery.ui.resizable');
    }


    /**
     * plugin - nestedsortable 추가 .
     */
    public function pluginNestedSortable()
    {
        $this->css_($this->sDefaultRootdir, '/plugins/nestedsortable/nestedSortable');

        $this->js_($this->sDefaultRootdir, '/plugins/ui/jquery.ui.core');
        $this->js_($this->sDefaultRootdir, '/plugins/ui/jquery.ui.widget');
        $this->js_($this->sDefaultRootdir, '/plugins/ui/jquery.ui.mouse');
        $this->js_($this->sDefaultRootdir, '/plugins/ui/jquery.ui.sortable');
        $this->js_($this->sDefaultRootdir, '/plugins/nestedsortable/jquery.ui.nestedSortable');
        $this->js_($this->sDefaultRootdir, '/plugins/nestedsortable/nestedTree');
    }

    /**
     * plugin - externalss
     */
    public function pluginExternals()
    {
        $this->js_($this->sDefaultRootdir, '/externals/jquery.bgiframe');
        $this->js_($this->sDefaultRootdir, '/externals/jquery.cookie');
        $this->js_($this->sDefaultRootdir, '/externals/jquery.dimensions');
        $this->js_($this->sDefaultRootdir, '/externals/jquery.easing');
        $this->js_($this->sDefaultRootdir, '/externals/jquery.metadata');
    }

    /**
     * plugin - 자동완성
     */
    public function pluginAutocomplete()
    {
        $this->insertJqueryUiCss();

        $this->js_($this->sDefaultRootdir, '/plugins/ui/jquery.ui.core');
        $this->js_($this->sDefaultRootdir, '/plugins/ui/jquery.ui.widget');
        $this->js_($this->sDefaultRootdir, '/plugins/ui/jquery.ui.position');
        $this->js_($this->sDefaultRootdir, '/plugins/ui/jquery.ui.autocomplete');
    }

    /**
     * plugin - JQuery UI 달력
     */
    public function pluginDatePicker()
    {
        $this->insertJqueryUiCss();

        $this->js_($this->sDefaultRootdir, '/plugins/ui/jquery.ui.core');
        $this->js_($this->sDefaultRootdir, '/plugins/ui/jquery.ui.widget');
        $this->js_($this->sDefaultRootdir, '/plugins/ui/jquery.ui.datepicker');
    }

    /**
     * plugin - JQuery 프로그레스바
     */
    public function pluginProgressbar()
    {
        $this->insertJqueryUiCss();

        $this->js_($this->sDefaultRootdir, '/plugins/ui/jquery.ui.core');
        $this->js_($this->sDefaultRootdir, '/plugins/ui/jquery.ui.widget');
        $this->js_($this->sDefaultRootdir, '/plugins/ui/jquery.ui.progressbar');
    }

    /**
     * plugin - 유효성 검사
     *
     * @param array $aOption
     * array(
     *     'form' => 폼 아이디
     *     'lang' => 언어코드
     *     'expand' => 에러 펼침여부
     *     'sslwrap' => Sslwrap JS 포함 여부
     * )
     */
    public function pluginValidator($aOption)
    {
        $sForm        = $aOption['form'] ? $aOption['form'] : false;
        $sLang        = $aOption['lang'] ? strtolower($aOption['lang']) : 'ko';
        $sExpand      = $aOption['expand'] === true ? 'true' : 'false';
        $isSslwrap    = $aOption['sslwrap'] === true ? true : false;

        $this->js_($this->sDefaultRootdir, '/plugins/validator/jquery.validator');
        $this->js_($this->sDefaultRootdir, '/plugins/validator/extends/jquery.validator.response');
        $this->js_($this->sDefaultRootdir, '/plugins/validator/extends/jquery.validator.helper');
        $this->js_($this->sDefaultRootdir, '/plugins/validator/extends/jquery.validator.handler');
        $this->js_($this->sDefaultRootdir, '/plugins/validator/extends/jquery.validator.verify');
        $this->js_($this->sDefaultRootdir, '/plugins/validator/extends/jquery.validator.filter');
        $this->js_($this->sDefaultRootdir, '/plugins/validator/locale/' . $sLang);

        if ($isSslwrap === true) {
            $this->pluginSslwrap();
        }

        if ($sForm) {
            $this->writeJS('
                $(document).ready(function(){
                    FwValidator.bind("' . $sForm . '", '. $sExpand .');
                });
            ');
        }
    }

    /**
     * plugin - AuthSSL 래퍼 - 데이터 암.복호화 플러그인
     */
    public function pluginSslwrap()
    {
        if ($this->sslwrap()->isFrontendEnabled() === false) {
            trigger_error('[Uipack] Sslwrap Plugin is disabled.', E_USER_WARNING);
            return;
        }

        $this->insertSslManager();

        $this->writeJS('
            Sslwrap._encryptInit({
            "reqKey" : "' . $this->sslwrap()->getFrontendEncryptReqKey() . '",
            "reqArrValuePrefix" : "' . $this->sslwrap()->getFrontendEncryptReqArrValuePrefix() . '",
            "reqArrValueSep" : "' . $this->sslwrap()->getFrontendEncryptReqArrValueSep() . '",
            "reqUnset" : "' . $this->sslwrap()->getFrontendEncryptReqUnset() . '"
            });
        ');
    }

    /**
     * SSL Frontend js 삽입
     */
    private function insertSslManager()
    {
        $sHost = $this->sslwrap()->getFrontendHost();

        $sScr1 = $sHost . '/crypt/AuthSSLManager.js';
        $sScr2 = $sHost . '/crypt/AuthSSLManager.plugin.js';

        $this->externalJS($sScr1);
        $this->externalJS($sScr2);

        $this->js_($this->sDefaultRootdir, '/plugins/sslwrap/jquery.sslwrap');
    }
}
